﻿Public Class FrmAuthManage
    Private Fuid As String
    Private Sub FrmAuthManage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GetUserList()
    End Sub
    Private Sub GetUserList()
        Try
            dg1.DataSource = Nothing
            Dim ErrString As String = String.Empty
            Dim ds As New DataSet
            Dim TmpArr As Byte() = Nothing
            If SControl._BASE_User_GetUserList(UInfo.FSession, "", TmpArr, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            If FControl.DsRestore(ds, TmpArr) = False Then
                MsgBox("无法解析返回数据!")
                Return
            End If
            dg1.DataSource = ds.Tables(0).DefaultView
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub GetAllAuth()
        If Fuid = Nothing Then
            Return
        End If
        Try
            dg2.Rows.Clear()
            dg3.Rows.Clear()
            Dim ErrString As String = String.Empty
            Dim ds As New DataSet
            Dim TmpArr() As Byte = Nothing
            If SControl._BASE_User_GetAllAuthList(UInfo.FSession, TmpArr, ErrString) = False Then
                MsgBox(ErrString)
            End If
            If FControl.DsRestore(ds, TmpArr) = False Then
                MsgBox("无法解析返回数据!")
                Return
            End If
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                dg2.Rows.Add(ds.Tables(0).Rows(i).Item("内码"), ds.Tables(0).Rows(i).Item("编号"), ds.Tables(0).Rows(i).Item("类别"), ds.Tables(0).Rows(i).Item("权限名"), ds.Tables(0).Rows(i).Item("权限说明"))
                dg3.Rows.Add(ds.Tables(0).Rows(i).Item("内码"), ds.Tables(0).Rows(i).Item("编号"), ds.Tables(0).Rows(i).Item("类别"), ds.Tables(0).Rows(i).Item("权限名"), ds.Tables(0).Rows(i).Item("权限说明"))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub GetUserAuth()
        If Fuid = Nothing Then
            Return
        End If
        Try
            Dim ErrString As String = String.Empty
            Dim ds As New DataSet
            Dim TmpArr() As Byte = Nothing
            If SControl._BASE_User_GetUserAuthList(UInfo.FSession, Fuid, TmpArr, ErrString) = False Then
                MsgBox(ErrString)
            End If
            If FControl.DsRestore(ds, TmpArr) = False Then
                MsgBox("无法解析返回数据!")
                Return
            End If
            For i As Integer = 0 To dg2.RowCount - 1
                Dim sid As String = dg2.Item(0, i).Value
                If ds.Tables(0).Select("内码=" & sid).Length > 0 Then
                    dg2.Rows(i).Visible = False
                Else
                    dg3.Rows(i).Visible = False
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellClick
        If dg1.SelectedCells.Count < 1 Then
            Return
        End If
        Try
            Fuid = dg1.Item(0, e.RowIndex).Value
            If Fuid = Nothing Then
                Return
            End If
            GetAllAuth()
            GetUserAuth()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub dg2_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg2.CellDoubleClick
        If dg2.SelectedCells.Count < 1 Then
            Return
        End If
        dg2.Rows(e.RowIndex).Visible = False
        dg3.Rows(e.RowIndex).Visible = True
    End Sub

    Private Sub dg3_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs)
        If dg3.SelectedCells.Count < 1 Then
            Return
        End If
        dg3.Rows(e.RowIndex).Visible = False
        dg2.Rows(e.RowIndex).Visible = True
    End Sub

    Private Sub ButtonX3_Click(sender As Object, e As EventArgs) Handles ButtonX3.Click
        Dispose()
    End Sub

    Private Sub ButtonX2_Click(sender As Object, e As EventArgs) Handles ButtonX2.Click
        Try
            For i As Integer = 0 To dg2.Rows.Count - 1
                dg2.Rows(i).Visible = True
                dg3.Rows(i).Visible = False
            Next
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        Try
            Dim ErrString As String = String.Empty
            Dim TmpId As String = String.Empty
            For i As Integer = 0 To dg3.Rows.Count - 1
                If dg3.Rows(i).Visible Then
                    '--授权
                    If TmpId = Nothing Then
                        TmpId += dg3.Item(0, i).Value.ToString
                    Else
                        TmpId += "|" & dg3.Item(0, i).Value.ToString
                    End If
                End If
            Next
            '--保存
            If SControl._BASE_User_ToAuth(UInfo.FSession, Fuid, TmpId, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            MsgBox("授权成功!")
            dg2.DataSource = Nothing
            dg3.DataSource = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub FrmAuthManage_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        GroupBox2.Width = (Width - GroupBox1.Width) / 2
    End Sub

    Private Sub dg3_CellDoubleClick_1(sender As Object, e As DataGridViewCellEventArgs) Handles dg3.CellDoubleClick
        If dg3.SelectedCells.Count < 1 Then
            Return
        End If
        dg3.Rows(e.RowIndex).Visible = False
        dg2.Rows(e.RowIndex).Visible = True
    End Sub
End Class